#!/bin/bash
programs=""
ip=""
centos=("gcc" "vsftpd" "ntpdate" "chkconfig" "wget" "httpd" "mysql" "mysql-server" "mysql-devel" "php" "php-common" "php-gd" "php-mbstring" "php-devel" "php-xml" "php-soap" "mod_ssl")

cancel()
{
	dialog --title "abortion" --clear --msgbox "problems detected while installing.\
	\nif you misoperated just restart the program.\
	\n\n\nif you encounter problems please get in touch with us.\
	\n\nweb:http://www.cnzxsoft.com/\nhotline:800-868-7722\nafter-sale:+86 551 6532 1158  /  +86 551 6532 1558" 20 75
}

ok()
{
	dialog --title "complete" --msgbox "Congratulations!All progress has been finished.\
	\nyou can access web page to check.\
	\n		衷心感谢玲玲同志对此次脚本的大力支持与鼓励\
	\n\nIDC/ISP:	https://IP:28443/\
		http://IP:28080/\
	\nCDN:					https://IP:28444/\
		http://IP:28081/\
	\nIRCS:				https://IP:28445/\
		http://IP:28082/\
	\nARMP:				http://IP:28099/\
	\nBAMS:				https://IP:28543/\
	\nNotify: check settings on both the zxsoft device and this server." 20 75
}

#show begin/end/subtitle/contents
show()
{
	status=$1
	post=$(cat ./install.log|wc -l)
	while :
	do
	pre=$post
	post=$(cat ./install.log|wc -l)
	if tail -6 ./install.log|grep next>>./tmp;
	then
		if [ $status -lt $2 ];then
			if [ $post -ne $pre ];then
				let  status+=1
			fi
		else
			status=$2
		fi
	elif tail -1 ./install.log|grep complete>/dev/null;
	then
		break
	fi
cat<<EOF
XXX
$3...\n
It will take some minutes...\n\n
`tail -10 ./install.log`
XXX
EOF
	echo $status
	done|dialog --title "installation" --clear --gauge "install..." 20 70 $status
	sleep 0.5
}

afterward()
{
cat>>./install.log<<EOF
Change Mysql password...
Mysql database is being import...
Grant privileges to devices...
Creating ftp account...
Shutdown selinux...
Shutdown Iptables...
Add crontab items...
...
EOF
	#do some configure
	show 100 "finishing installation..."
	
	cp ./script/* /usr/local/sbin/
	chmod +x /usr/local/sbin/*
	
	mysql -u root mysql <<EOF
	UPDATE user SET Password=PASSWORD('zxsoft') where USER='root';
	FLUSH PRIVILEGES;
EOF

	for item in $(head -1 ./install.log);
	do
		if [ "$item" == "IDC/ISP" ];
		then 
			mysql -uroot -pzxsoft -h127.0.0.1 -P28096 <<EOF
			source ./jdis-isms/isms.sql;
EOF
		if [ -f /etc/php.ini ]; then
			sed -i 's/'"upload_max_filesize = 2M"'/'"upload_max_filesize = 20M"'/g' /etc/php.ini
		fi;
			cat >> "/etc/init/jagent.conf" <<EOF
			# jagent program
			#
			start on runlevel [2345]

			stop on runlevel [S016]

			respawn
			exec /usr/local/sbin/jagent
EOF
			cat >> "/etc/init/zxagent.conf" <<EOF
			# zxagent program
			#
			start on runlevel [2345]

			stop on runlevel [S016]

			respawn
			exec /usr/local/sbin/zxagent
EOF
			initctl start jagent
			initctl start zxagent
			
			if [ ! -d /etc/jdis ]; then
				mkdir -r /etc/jdis;
			fi;
			cp -a ./etc/domain_postfix.conf /etc/jdis
			cp -a ./etc/isms_icp.ini /etc/jdis
		
		elif [ "$item" == "CDN" ];
		then 
			mysql -uroot -pzxsoft -h127.0.0.1 -P28096 <<EOF
			source ./jdis-isms-cdn/isms-cdn.sql;
EOF
			if [ ! -d /etc/jdis_cdn ]; then
				mkdir -r /etc/jdis_cdn;
			fi;
			cp -a ./etc/domain_postfix.conf /etc/jdis_cdn
			cp -a ./etc/isms_icp.ini /etc/jdis_cdn

			cat >> "/etc/init/jagent_cdn.conf" <<EOF
			# jagent program
			#
			start on runlevel [2345]

			stop on runlevel [S016]

			respawn
			exec /usr/local/sbin/jagent_cdn
EOF
			initctl start jagent_cdn
		
		elif [ "$item" == "IRCS" ];
		then 
			mysql -uroot -pzxsoft -h127.0.0.1 -P28096 <<EOF
			source ./jdis-isms-ircs/isms-ircs.sql;
EOF
			if [ ! -d /etc/jdis_ircs ]; then
				mkdir -r /etc/jdis_ircs;
			fi;
			cp -a ./etc/domain_postfix.conf /etc/jdis_ircs
			cp -a ./etc/isms_icp.ini /etc/jdis_ircs
			
			cat >> "/etc/init/jagent_ircs.conf" <<EOF
			# jagent program
			#
			start on runlevel [2345]

			stop on runlevel [S016]

			respawn
			exec /usr/local/sbin/jagent_ircs
EOF

		elif [ "$item" == "ARMP" ];
		then 
			mysql -uroot -pzxsoft -h127.0.0.1 -P28096 <<EOF
			source ./armp.sql;
EOF
		elif [ "$item" == "BAMS" ];
		then 
			mysql -uroot -pzxsoft -h127.0.0.1 -P28096 <<EOF
			source ./bams.sql;
EOF
			cat >> /var/spool/cron/apache <<EOF
			*/1 * * * * php /var/www/jdis-bams/web/public/download.php > /dev/null
EOF
		fi
	done
	
	for item in $(sed -n 2p ./install.log);
	do
		mysql -u root -pzxsoft -h127.0.0.1 -p28096 <<EOF
		grant all privileges on isms.* to 'isms'@'$item' identified by 'zxsoft';
		FLUSH PRIVILEGES;
EOF
	done
	
	setenforce 0 > /dev/null;
	if [ -f /etc/sysconfig/selinux ]; then
		sed -i 's/'"SELINUX=enforcing"'/'"SELINUX=disabled"'/g' /etc/sysconfig/selinux;
	fi;
	
	service iptables stop > /dev/null 2>&1;
	chkconfig iptables off > /dev/null 2>&1;
	
	chkconfig httpd on > /dev/null 2>&1;
	chkconfig mysqld on > /dev/null 2>&1;
	chkconfig vsftpd on > /dev/null 2>&1;
	
	#时区设置
	cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
	ntpdate aisa.pool.ntp.org
	#替换mysql配置文件
	if [ -f /etc/my.cnf ]; then
		cp /etc/my.cnf /etc/my.cnf.bak
		cp ./etc/my.cnf /etc/my.cnf
	fi;
	if [ -f /etc/vsftpd/vsftpd.conf ]; then
		cp ./etc/vsftpd.conf /etc/vsftpd/vsftpd.conf
	fi;
	#本地ftpd设置
	/usr/sbin/adduser -d /opt/test_ftp -g ftp -s /sbin/nologin zxftp
	echo "zxsoft" | passwd --stdin "zxftp"
	cp ./etc/ssl.conf /etc/httpd/conf.d/
	
	if [ ! -d /etc/httpd/ssl ]; then
		mkdir -r /etc/httpd/ssl;
	fi;
	cp -a ./etc/server.crt /etc/httpd/ssl
	cp -a ./etc/server.key-rsa /etc/httpd/ssl

	#重启mysqld
	service mysqld restart > /dev/null 2>&1;
	#重启vsftpd
	service vsftpd restart > /dev/null 2>&1;
	
	cat >> /var/spool/cron/root <<EOF
*/1 * * * * /usr/local/sbin/jfind
0 */1 * * * /usr/local/sbin/jicp
*/1 * * * * /usr/local/sbin/jfind_cdn
0 */1 * * * /usr/local/sbin/jicp_cdn
*/1 * * * * /usr/local/sbin/jfind_ircs
0 */1 * * * /usr/local/sbin/jicp_ircs
1 3 * * * /usr/local/sbin/clean_acclog.sh
1 2 * * * /usr/local/sbin/clean_report.sh
1 3 * * * /usr/local/sbin/clean_acclog_cdn.sh
1 2 * * * /usr/local/sbin/clean_report_cdn.sh
1 3 * * * /usr/local/sbin/clean_acclog_ircs.sh
1 2 * * * /usr/local/sbin/clean_report_ircs.sh
1 1 * * * /usr/local/sbin/clean_uploadfile.sh
* * * * 0 /usr/local/sbin/clean_mail_root.sh
30 23 * * * ntpdate asia.pool.ntp.org
EOF
	ok
}

installation()
{
	if [ ! -d /var/www ]; then
		mkdir -p /var/www
	fi;
	{
	for item in $(head -1 ./install.log);
	do
		echo "install $item" >>./install.log
		if [[ $item =~ "IDC" ]];
		then
			#cp web
			cp -r ./jdis-isms /var/www/
			echo "copy web into /var/www...">>./install.log
			chown apache.apache `find /var/www/jdis-isms/web/data -type d`;
			chown apache.apache `find /var/www/jdis-isms/web/data -type f`;
			chmod 0755 `find /var/www/jdis-isms/web -type d`;
			chmod 0644 `find /var/www/jdis-isms/web -type f`;
			#install ISMS
			cd ./tools/jagent
			make >>../../install.log
			cp jagent /usr/local/sbin/
			echo "installed jagent...">>../../install.log
			echo "">>../../install.log
			
			cd ../jfind
			make >>../../install.log
			cp jfind /usr/local/sbin/
			echo "installed jfind...">>../../install.log
			echo "">>../../install.log
			
			cd ../jicp
			make >>../../install.log
			cp jicp /usr/local/sbin/
			echo "installed jicp...">>../../install.log
			echo "">>../../install.log
			
			cd ../zxagent
			make >>../../install.log
			cp zxagent /usr/local/sbin/
			echo "installed zxagent...">>../../install.log
			echo "">>../../install.log
			cd ../../
		elif [[ $item == "CDN" ]];
		then
			#cp web
			cp -r ./jdis-isms-cdn /var/www/
			echo "copy web into /var/www...">>./install.log
			chown apache.apache `find /var/www/jdis-isms-cdn/web/data -type d`;
			chown apache.apache `find /var/www/jdis-isms-cdn/web/data -type f`;
			chmod 0755 `find /var/www/jdis-isms-cdn/web -type d`;
			chmod 0644 `find /var/www/jdis-isms-cdn/web -type f`;
			#install CDN
			cd ./tools/jagent_cdn
			make >>../../install.log
			cp jagent_cdn /usr/local/sbin/
			echo "installed jagent_cdn...">>../../install.log
			echo "">>../../install.log
			
			cd ../jfind_cdn
			make >>../../install.log
			cp jfind_cdn /usr/local/sbin/
			echo "installed jfind_cdn...">>../../install.log
			echo "">>../../install.log
			
			cd ../jicp_cdn
			make >>../../install.log
			cp jicp_cdn /usr/local/sbin/
			echo "installed jicp_cdn...">>../../install.log
			echo "">>../../install.log
			cd ../../
		elif [[ $item == "IRCS" ]]
		then
			#cp web
			cp -r ./jdis-isms-ircs /var/www/
			echo "copy web into /var/www...">>./install.log
			chown apache.apache `find /var/www/jdis-isms-ircs/web/data -type d`;
			chown apache.apache `find /var/www/jdis-isms-ircs/web/data -type f`;
			chmod 0755 `find /var/www/jdis-isms-ircs/web -type d`;
			chmod 0644 `find /var/www/jdis-isms-ircs/web -type f`;
			#install IRCS
			cd ./tools/jagent_ircs
			make >>../../install.log
			cp jagent_ircs /usr/local/sbin/
			echo "installed jagent_ircs...">>../../install.log
			echo "">>../../install.log
			
			cd ../jfind_ircs
			make >>../../install.log
			cp jfind_cdn /usr/local/sbin/
			echo "installed jfind_ircs...">>../../install.log
			echo "">>../../install.log
			
			cd ../jicp_ircs
			make >>../../install.log
			cp jicp_ircs /usr/local/sbin/
			echo "installed jicp_ircs...">>../../install.log
			echo "">>../../install.log
			cd ../../
		elif [[ $item == "ARMP" ]]
		then
			#cp web
			cp -r ./jdis-armp /var/www/
			echo "copy web into /var/www...">>./install.log
			chown apache.apache `find /var/www/jdis-armp/web/data -type d`;
			chown apache.apache `find /var/www/jdis-armp/web/data -type f`;
			chmod 0755 `find /var/www/jdis-armp/web -type d`;
			chmod 0644 `find /var/www/jdis-armp/web -type f`;
			#install ARMP
			echo "installed armp...">>../../install.log
			echo "">>../../install.log
		elif [[ $item == "BAMS" ]]
		then
			#cp web
			cp -r ./jdis-bams /var/www/
			echo "copy web into /var/www...">>./install.log
			chown apache.apache `find /var/www/jdis-armp/web/data -type d`;
			chown apache.apache `find /var/www/jdis-armp/web/data -type f`;
			chmod 0755 `find /var/www/jdis-armp/web -type d`;
			chmod 0644 `find /var/www/jdis-armp/web -type f`;
			#install ARMP
			echo "installed bams...">>../../install.log
			echo "">>../../install.log
		fi;
	done
	}&
	show 60 90 "install programs"
	afterward
}

depends()
{
	{
	for item in ${centos[*]};
	do
		echo $item >>./install.log
		echo "next">>./install.log
		yum install $item -y>>./install.log
		if [ "$item" == "mod_ssl" ];
		then 
			echo "install depends complete.">>./install.log
		fi
	done
	}&
	show 0 60 "install depends"
	installation
}

confirm()
{
	echo "">>install.log
	dialog --title "check configure" --clear --yesno "Be careful to check the configure below:\
	\n\n Programs:$(head -1 ./install.log)\
	\n IP address:$(sed -n 2p ./install.log)\
	\n\nchoose <Yes> to start installitation.\nchoose <No> to get back.\
	\nexit by <ESC><ESC>." 20 75
	if [ $? -eq 0 ];
	then
		depends
	else
		cancel
	fi
}

address()
{
	echo "">>./install.log
	dialog --title "device address" --clear --inputbox "The grant privileges process is need by the program to make it work normally.\
	\nyou may have more than one device, however, just IP address is needed.
	\ninput your IP list, split with space between eath other." 20 75 2>>./install.log
	if [ $? -eq 0 ];
	then
		confirm
	else
		cancel
	fi
}

start()
{
	dialog --title "pre installation" --clear --checklist \
	"please choose the program that you want to install.\n tap <Space> to choose, tap <Enter> to continue." 20 75 5 \
	IDC/ISP "Internet Service Provider." 1 \
	CDN	 "Content Delivery Network." 2 \
	IRCS	 "Internet Resource Collaboration Service." 3 \
	BAMS	 "IDC/ISP Register." 4 \
	ARMP	 "Internet Access provider." 5 2> ./install.log
	if [ $? -eq 0 ];
	then
		address
	else
		cancel
	fi
}
#start
#echo ""
#echo "[Notifition] the script depends program <dialog>, would you like to install it?"
#read -p "[yes/no]:" choose
#if [ X"$choose" == X"yes" ]; then
#	yum install dialog -y
#	start
#else
#	echo "Thank you, bye!"
#fi

installation
